﻿#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void Initboard(char board[ROWS][COLS], int rows, int cols, char set)
{
	int i;
	int j;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

void Dispalyboard(char board[ROWS][COLS], int row, int col)
{
	int i;
	int j;
	printf("-----------扫雷游戏-----------\n");
	for (j = 0; j <=col; j++)
	{
		printf("%d ",j);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("-----------扫雷游戏-----------\n");
}
void Setmine(char board[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}
int get_mine_count(char board[ROWS][COLS], int x, int y)
{
	return (board[x - 1][y - 1] +
		board[x - 1][y] +
		board[x - 1][y + 1] +
		board[x][y - 1] +
		board[x][y + 1]+
		board[x+1][y]+
		board[x + 1][y - 1] +
		board[x + 1][y + 1] - 8 * '0');
}
//展开一片(递归)
void unfold(char show[ROWS][COLS],char mine[ROWS][COLS], int x,int y,int *new_count)
{
	if (mine[x][y] == '1')
		return ;
	if (show[x][y]  != '*')
		return;
	int new_mine = get_mine_count(mine, x, y);
		if (new_mine > 0)
		{
			show[x][y] = new_mine + '0';
			(*new_count)++;
			return;
		}
		else
		{
			show[x][y] = ' ';
			unfold(show, mine, x - 1, y - 1,  new_count);  
			unfold(show, mine, x - 1, y + 1,  new_count); 
			unfold(show, mine, x, y - 1,  new_count); 
			unfold(show, mine, x, y + 1, new_count); 
			unfold(show, mine, x + 1, y - 1, new_count);  
			unfold(show, mine, x + 1, y,  new_count);  
			unfold(show, mine, x + 1, y + 1, new_count);
		}
	}
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	int x = 0;
	int y = 0;
	int win = 0;
	char sign;
	while (win < row * col - EASY_COUNT&&count>0)
	{
		printf("请输入坐标,c代表排雷，m代表插标,u代表取消标记\n");
		scanf(" %c %d %d", &sign,&x, &y);
		if(sign=='c')
		{
			if (x >= 1 && x <= row && y >= 1 && y <= col)
			{
				
				if (show[x][y] != '*')
				{
					printf("已经排查过此坐标，请勿重复排查\n");
				}
				else
				{
					if (mine[x][y] == '1')//如果是雷
					{
						printf("很遗憾，你被炸死了，游戏结束\n");
						Dispalyboard(mine, ROW, COL);
						break;
					}
					else //如果不是雷
					{
						int new_count = 0;
						unfold(show, mine, x, y,&new_count);
						win+=new_count;
						int mine_count = get_mine_count(mine, x, y);
						show[x][y] = mine_count + '0';
						Dispalyboard(show, ROW, COL);
					}
				}
			}
			else
			{
				printf("坐标非法，请重新输入\n");
			}
		}
		else if (sign == 'm') 
		{
			if (x >= 1 && x <= row && y >= 1 && y <= col)
			{
				if (show[x][y] == '*' && mine[x][y] != '1')
				{
					show[x][y] = '!';
					Dispalyboard(show, ROW, COL);
				}
				else if (show[x][y] == '*' && mine[x][y] == '1')//如果是雷
					{
						show[x][y] = '!';
						int ret = 0;
						ret = --count;
						printf("一共有%d个雷，现在还剩%d个雷\n", EASY_COUNT, count);
						Dispalyboard(show, ROW, COL);
					}
				else if (show[x][y] == '!')
				{
					printf("此处已经插标，请勿重复插标\n");
				}
			}
			else
			{
				printf("坐标非法，请重新输入\n");
			}
		}
		else if (sign == 'u')
		{
			if (x >= 1 && x <= row && y >= 1 && y <= col)
			{
				if (show[x][y] == '!')
				{
					if (mine[x][y] == '1')
					{
						count++;
					}
					show[x][y] = '*';
					Dispalyboard(show, ROW, COL);
					printf("已取消标记\n");
				}
				else
					printf("此处未标记，不需要取消\n");
			}
			else
				printf("坐标非法，请重新输入\n");
		}
	}
	if (win == row * col - EASY_COUNT)
		printf("恭喜你排查了所有安全区域,游戏胜利\n");
	if (count==0)
		printf("恭喜你找出所有雷，游戏胜利\n");
}